package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.anno.Log;
import org.example.pojo.*;
import org.example.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @Log
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        System.out.println("添加班级");
        clazzService.insert(clazz);
        return Result.success();
    }

    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        log.info("分页查询班级列表");
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        log.info("结果:{}", pageResult);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("获取班级信息,id:{}", id);
        Clazz clazz = clazzService.getById(id);
        log.info("结果:{}", clazz);
        return Result.success(clazz);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级信息:{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级,ids:{}", id);
        clazzService.delete(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        List<Clazz> clazzList = clazzService.list();
        return Result.success(clazzList);
    }
}
